<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mobile_Hotel_module extends CI_Module {

    public $data;
    public $sid;
    private $period_price_array;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Orderhotel_model');
        $this->load->model('member.User_model');
        $this->sid = $GLOBALS['sid'];
        $this->data =$GLOBALS['config'];
        $this->load->model('weixin.weixin_model');
        $this->setting = $this->weixin_model->db_get_setting();
        define('TOKEN',     @$this->setting['token']);
        define('APPID',     @$this->setting['appid']);
        define('APPSECRET', @$this->setting['appsecret']);

        $this->period_price_array = array(
            $GLOBALS['config']['week_unit_price'],
            $GLOBALS['config']['mon_unit_price'],
            $GLOBALS['config']['tue_unit_price'],
            $GLOBALS['config']['wed_unit_price'],
            $GLOBALS['config']['thu_unit_price'],
            $GLOBALS['config']['fri_unit_price'],
            $GLOBALS['config']['sat_unit_price']
        );

    }


    public function book()
    {



        $user_id = $this->session->userdata('user_id');
        $this->session->set_userdata('redirect_uri',site_url('mobile/hotel/book'));
        if(!intval($user_id))  redirect('mobile/oauth/oauth');


        $username = $this->session->userdata('username');


        $check = false;
        $info = $this->User_model->_user_info($user_id);

        if (!empty($info['mobile']) && !empty($info['realname'])) $check = true;

        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {

            $start_date = $_POST['start_date'];
            $end_date = $_POST['end_date'];
            @$male_num = intval($_POST['male_num']);
            @$woman_num = intval($_POST['woman_num']);
            @$child_num = intval($_POST['child_num']);
            @$contact_name = trim($_POST['name']);
            @$contact_phone = trim($_POST['phone']);
            @$note = trim($_POST['note']);


            $days = (strtotime($end_date)-strtotime($start_date))/24/60/60;
            $order_account =  ($male_num + $woman_num + $child_num) * $days * $this->data['book_unit_price'] ;
           
            //订单信息
            $order_data = array(
                'order_sn' => $this->Orderinfo_model->set_order_sn(),
                'order_type' => 1,//酒店订单
                'cat_type' => 2,//手机订单
                'order_account' => $order_account,
                'room_account' => $order_account,
                'earnest_account' => $order_account / 3,
                'order_status' => 1, //酒店需要审核
                'pay_status' => 1,//未支付
                'order_note' => $note,
                'add_time' => date('Y-m-d H:i:s', time()),
                'user_id' => $user_id,//用户ID
                'admin_user_id' => '0' //管理员ID
            );
            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if ($order_id)
            {
                //联系人信息
                $contact_data = array(
                    'order_id' => $order_id,
                    'name' => $contact_name,
                    'email' => '',
                    'phone_num' => $contact_phone,
                    'wx_nick' => $username
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

                //订单酒店信息

                $row = array(
                    'sid' => $this->sid,
                    'order_id' => $order_id,
                    'number' => $male_num + $woman_num + $child_num,
                    'days'=>$days,
                    'male_number' => $male_num,
                    'woman_number' => $woman_num,
                    'child_number' => $child_num,
                    'start_date' => $start_date,
                    'end_date' => $end_date

                );

                $return_status = $this->Orderhotel_model->add_row($row);


                if ($contact_return_status && $return_status)
                {
//                    $this->User_model->_send_sms($this->data['notify_num'],'SMS_10885119',array());

                    $replace = array('sname' => $this->data['company'], 'name' => $contact_name, 'date' => $start_date, 'phone' => $contact_phone);
                    $this->User_model->_send_sms($this->data['notify_num'], 'SMS_12760115', $replace);

                    return json_encode(array('status' => '1', 'info' => '提交订单成功!', 'msg' => 'order_fail'));
                }
                else
                {
                    return json_encode(array('status' => '0', 'info' => '订单生成失败!', 'msg' => 'order_fail'));
                }
            }
            else
            {
                return json_encode(array('status' => '0', 'info' => '订单生成失败!', 'msg' => 'order_fail'));
            }


        }

        $this->data['check'] = $check;
        $this->data['info'] = $info;
        $this->data['common'] = array(
            array(
                'head' => '个人中心',
                'nav' => 'center',
                'title' => '酒店预定',
                'noshare' => '1'
            )
        );
        $this->data['form_url'] = site_url('mobile/hotel/book');
        $view = MOBILE_TEMPLATE_NAME . '/' . 'hotel_order';
        if($this->data['sell_model'] == 2)
        {
            $this->data['form_url'] = site_url('mobile/hotel/mix_save');
            $view = MOBILE_TEMPLATE_NAME . '/' . 'hotel_order_mix';
        }

        $this->data['complete_url'] = site_url('mobile/member/edit_complete_member');
        $this->load->view($view, $this->data);
    }

    public function mix_save()
    {
        $user_id = $this->session->userdata('user_id');
        $username = $this->session->userdata('username');
        $start_date = $_POST['start_date'];
        $end_date = $_POST['end_date'];
        @$male_num = intval($_POST['male_num']);
        @$woman_num = intval($_POST['woman_num']);
        @$child_num = intval($_POST['child_num']);
        @$contact_name = trim($_POST['name']);
        @$contact_phone = trim($_POST['phone']);
        @$note = trim($_POST['note']);

        @$order_account = trim($_POST['order_account']);
        $days = (strtotime($end_date)-strtotime($start_date))/24/60/60;

        //订单信息
        $order_data = array(
            'order_sn' => $this->Orderinfo_model->set_order_sn(),
            'order_type' => 1,//酒店订单
            'cat_type' => 2,//手机订单
            'order_account' => $order_account,
            'room_account' => $order_account,
            'earnest_account' => $order_account / 3,
            'order_status' => 1, //酒店需要审核
            'pay_status' => 1,//未支付
            'order_note' => $note,
            'add_time' => date('Y-m-d H:i:s', time()),
            'user_id' => $user_id,//用户ID
            'admin_user_id' => '0' //管理员ID
        );
        $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

        if ($order_id)
        {
            //联系人信息
            $contact_data = array(
                'order_id' => $order_id,
                'name' => $contact_name,
                'email' => '',
                'phone_num' => $contact_phone,
                'wx_nick' => $username
            );
            $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

            //订单酒店信息

            $row = array(
                'sid' => $this->sid,
                'order_id' => $order_id,
                'number' => $male_num + $woman_num + $child_num,
                'days'=>$days,
                'male_number' => $male_num,
                'woman_number' => $woman_num,
                'child_number' => $child_num,
                'start_date' => $start_date,
                'end_date' => $end_date

            );

            $return_status = $this->Orderhotel_model->add_row($row);


            if ($contact_return_status && $return_status)
            {

                $replace = array('sname' => $this->data['company'], 'name' => $contact_name, 'date' => $start_date, 'phone' => $contact_phone);
                $this->User_model->_send_sms($this->data['notify_num'], 'SMS_12760115', $replace);

                return json_encode(array('status' => '1', 'info' => '提交订单成功!', 'msg' => 'order_fail'));
            }
            else
            {
                return json_encode(array('status' => '0', 'info' => '订单生成失败!', 'msg' => 'order_fail'));
            }
        }
        else
        {
            return json_encode(array('status' => '0', 'info' => '订单生成失败!', 'msg' => 'order_fail'));
        }
    }


    public function calc_mix_order_account_ajax()
    {

        $start = $this->input->get_post('start');
        $end = $this->input->get_post('end');
        $numbers = $this->input->get_post('numbers');

        $date_section = fomate_range_date($start, $end); // 日期区间

        $account = 0 ;
        foreach ($date_section as $index => $item)
        {
            $account += $numbers * $this->period_price_array[date('w',strtotime($item))];
        }

        $return = array('status'=>1,'account'=>$account);
        echo json_encode($return);

    }
}